package com.appkubes.mall.restful.application.goods.taobao.service.impl;

import com.appkubes.mall.core.entity.MallGoodsTaobao;
import com.appkubes.mall.core.service.impl.BaseMallGoodsTaobaoServiceImpl;
import com.appkubes.mall.restful.application.goods.taobao.dao.GoodsTaobaoDao;
import com.appkubes.mall.restful.application.goods.taobao.entity.dto.GoodsTaobaoDetailsDto;
import com.appkubes.mall.restful.application.goods.taobao.entity.dto.GoodsTaobaoListDto;
import com.appkubes.mall.restful.application.goods.taobao.entity.vo.GoodsDetailsVO;
import com.appkubes.mall.restful.application.goods.taobao.entity.vo.RealTimeListVO;
import com.appkubes.mall.restful.application.goods.taobao.model.GoodsTaobaoModel;
import com.appkubes.mall.restful.application.goods.taobao.service.GoodsTaobaoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.taobao.api.ApiException;
//import com.taobao.api.DefaultTaobaoClient;
//import com.taobao.api.TaobaoClient;
//import com.taobao.api.request.TbkTpwdCreateRequest;
//import com.taobao.api.response.TbkTpwdCreateResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 淘宝商品
 * @Date: 2020-01-12
 * @Version: V1.0
 */
@Service
public class GoodsTaobaoServiceImpl extends BaseMallGoodsTaobaoServiceImpl implements GoodsTaobaoService {
    @Autowired
    GoodsTaobaoDao goodsTaobaoDao;

    @Value("${taobao.serverUrl}")
    private String serverUrl;

    @Value("${taobao.appkey}")
    private String appKey;

    @Value("${taobao.appSecret}")
    private String appSecret;

    @Override
    public IPage<GoodsTaobaoListDto> getList(Page<MallGoodsTaobao> pageInfo, Integer cid, Integer minPrice, Integer maxPrice, Integer minCoupon, Integer maxCoupon, Integer tkRate, String sort, String keyword) {
        QueryWrapper<MallGoodsTaobao> wrapper = new QueryWrapper<>();
        if(keyword != null){
            wrapper.like("title",keyword);
        }
        if(cid!=null){
            wrapper.eq("cid",cid);
        }
        if(minPrice != null){
            wrapper.ge("actual_price",minPrice);
        }
        if(maxPrice != null){
            wrapper.le("actual_price",maxPrice);
        }
        if(minCoupon != null){
            wrapper.ge("coupon_price",minCoupon);
        }
        if(maxCoupon != null){
            wrapper.le("coupon_price",maxCoupon);
        }
        if(tkRate != null){
            wrapper.ge("commission_rate",tkRate);
        }
        if(sort != null){
            switch (sort){
                case "zx"://最新
                    wrapper.orderByDesc("create_time");
                    break;
                case "sell"://销量
                    wrapper.orderByDesc("month_sales");
                    break;
                case "lql"://领券量
                    wrapper.orderByDesc("coupon_receive_num");
                    break;
                case "yj"://佣金
                    wrapper.orderByDesc("commission_rate");
                    break;
                case "jgdown"://佣金降序
                    wrapper.orderByDesc("actual_price");
                    break;
                case "jgup"://佣金升序
                    wrapper.orderByAsc("actual_price");
                    break;
                case "zh"://综合
                    break;
            }
        }
        IPage<MallGoodsTaobao> goodsList = goodsTaobaoDao.selectPage(pageInfo, wrapper);
        return transform(goodsList);
    }


    @Override
    public IPage<GoodsTaobaoListDto> getList(Page<MallGoodsTaobao> pageInfo, String brandId) {
        QueryWrapper<MallGoodsTaobao> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",brandId);
        IPage<MallGoodsTaobao> goodsList = goodsTaobaoDao.selectPage(pageInfo, wrapper);
        return transform(goodsList);
    }

    @Override
    public IPage<GoodsTaobaoListDto> getBoutique(Page<GoodsTaobaoModel> pageInfo) {
        IPage<MallGoodsTaobao> goodsList = goodsTaobaoDao.getBoutique(pageInfo);
        return transform(goodsList);
    }

    @Override
    public IPage<GoodsTaobaoListDto> getSaleRanking(RealTimeListVO realTimeListVO) {
        Page<MallGoodsTaobao> page = new Page<>(realTimeListVO.getPage(), realTimeListVO.getSize());

        MallGoodsTaobao goodsTaobao = new MallGoodsTaobao();
        goodsTaobao.setCid(realTimeListVO.getCid());

        QueryWrapper<MallGoodsTaobao> wrapper = new QueryWrapper<>();
        wrapper.setEntity(goodsTaobao);
        if(realTimeListVO.getSortType() != null){
            switch (realTimeListVO.getSortType()){
                case "month_sales"://月销量
                    wrapper.orderByDesc("month_sales");
                    break;
                case "daily_sales"://销量
                    wrapper.orderByDesc("daily_sales");
                    break;
                case "two_hours_sales"://领券量
                    wrapper.orderByDesc("two_hours_sales");
                    break;
                case "hot_push"://佣金
                    wrapper.orderByDesc("hot_push");
                    break;
                default:
                    break;
            }
        }
        IPage<MallGoodsTaobao> goodsList = goodsTaobaoDao.selectPage(page, wrapper);
        return transform(goodsList);
    }

    @Override
    public GoodsTaobaoDetailsDto getDetails(GoodsDetailsVO goodsDetailsVO) {
        MallGoodsTaobao goodsTaobao = new MallGoodsTaobao();
        goodsTaobao.setGoodsId(goodsDetailsVO.getGoodsId());
        MallGoodsTaobao goods = this.getOneByEntity(goodsTaobao);
        GoodsTaobaoDetailsDto goodsTaobaoDetailsDto = new GoodsTaobaoDetailsDto();
        BeanUtils.copyProperties(goods,goodsTaobaoDetailsDto);
        return goodsTaobaoDetailsDto;
    }

    /**
     * 表的实体类对象，转换成接口实体类对象
     * @param goodsList
     * @return
     */
    private IPage<GoodsTaobaoListDto> transform(IPage<MallGoodsTaobao> goodsList){
        IPage<GoodsTaobaoListDto> list = new Page<>();
        list.setTotal(goodsList.getTotal());
        list.setCurrent(goodsList.getCurrent());
        list.setPages(goodsList.getPages());
        list.setSize(goodsList.getSize());

        List<GoodsTaobaoListDto> tmpList = new ArrayList<>();
        for (MallGoodsTaobao goods:goodsList.getRecords()
        ) {
            GoodsTaobaoListDto tmpGoods = new GoodsTaobaoListDto();
            BeanUtils.copyProperties(goods,tmpGoods);
            tmpList.add(tmpGoods);
        }
        list.setRecords(tmpList);
        return list;
    }

}
